using UnityEngine;
using System.Collections;

public class ManagerHUD : MonoBehaviour
{
	/// <summary>
	/// Anel 1, anel 2, anel 3 da HUD
	/// </summary>
	[SerializeField]
	private ManagerAnimationUV rings1 = null, rings2 = null, rings3 = null;
	
	//Coracao da HUD
	[SerializeField]
	private ManagerAnimationUV heart;
	
	//Glow da HUD
	[SerializeField]
	private Renderer glow;
	
	//Personagem para mostrar o HP
	[SerializeField]
	private CharacterBehaviour characterAtt;
	
	//Som para tocar quando entrar na zona de especial, quando sair, quando entrar na zona de morte
	[SerializeField]
	private AudioClip enterOnAlertZone = null, exitOnAlertZone = null, deadOnAlert = null, deadMinOnAlert = null;
	
	//Máximo para manter no estado normal
	private float maxToNormalWork = 75;
	
	//Tempo entre a dupla de batida no máximo, tempo entre batidas no máxmo
	private float timeBetweenBeatsOnMax = 0.1f, timeBetweenDoubleOnMax = 0.05f;
	//Tempo entre a dupla de batida no mínimo, tempo entre batidas no mínimo
	private float timeBetweenBeatsOnMin = 2f, timeBetweenDoubleOnMin = 1f;
	
	//Tempo atual entre a dupla de batida, tempo entre batidas
	private float timeBetweenBeat = 0f, timeBetweenDouble = 0f;
	
	//Porcentagem no em relacao ao máximo
	private float percentage = 1;
	
	//HP anterior
	private float oldHP;
	
	//Tocador para HUD
	[SerializeField]
	private AudioSource heartSource;
	[SerializeField]
	private AudioSource[] audioSourcesToMuteOnTenseMoment;
	
	void Start()
	{
		StartCoroutine(WaitBetweenBeats());
	}
	
	void Update()
	{
		if(characterAtt.hp > maxToNormalWork)
		{
			rings1.transform.Rotate(Vector3.forward * 1000 * Time.deltaTime);
			rings3.transform.Rotate(Vector3.forward * 1000 * Time.deltaTime);
			rings2.transform.Rotate(Vector3.forward * -1000 * Time.deltaTime);
		}
		else
		{
			rings1.transform.localEulerAngles = Vector3.zero;
			rings2.transform.localEulerAngles = Vector3.zero;
			rings3.transform.localEulerAngles = Vector3.zero;
			
			percentage = characterAtt.hp/maxToNormalWork;
			timeBetweenBeat = timeBetweenBeatsOnMin - ((timeBetweenBeatsOnMin - timeBetweenBeatsOnMax) * percentage);
			timeBetweenDouble = timeBetweenDoubleOnMin - ((timeBetweenDoubleOnMin - timeBetweenDoubleOnMax) * percentage);
		}
		
		if(characterAtt.hp < 25)
		{
			iTween.ColorUpdate(heart.gameObject, Color.red, 1);
			iTween.ColorUpdate(glow.gameObject, new Color(1, 0, 0, percentage), 1);
		}
		else if(characterAtt.hp >= 75)
		{
			iTween.ColorUpdate(heart.gameObject, new Color(0.19f, 0.6f, 0.9f), 1);
			iTween.ColorUpdate(glow.gameObject, new Color(0.19f, 0.6f, 0.9f, percentage), 1);
		}
		else
		{
			iTween.ColorUpdate(heart.gameObject, Color.green, 1);
			iTween.ColorUpdate(glow.gameObject, new Color(0, 1, 0, percentage), 1);
		}
		
		if(oldHP != characterAtt.hp)
		{
			if(oldHP < 75 && characterAtt.hp >= 75)
			{
				//Enter...
				audio.PlayOneShot(enterOnAlertZone);
			}
			else if(oldHP >= 75 && characterAtt.hp < 75)
			{
				//Exit...
				audio.PlayOneShot(exitOnAlertZone);
			}
			
			if(characterAtt.hp >= 0)
			{
				heart.GoToFrameOf("size", (int)characterAtt.hp);
			}
			
			oldHP = characterAtt.hp;
		}
		
		if(characterAtt.hp < 25)
		{
			audio.volume = Mathf.Max(0, audio.volume - Time.deltaTime);
			
			foreach(AudioSource a in audioSourcesToMuteOnTenseMoment)
			{
				a.volume = Mathf.Max(0, audio.volume - Time.deltaTime);
			}
			
			if(heartSource.clip != deadMinOnAlert)
			{
				heartSource.clip = deadMinOnAlert;
				heartSource.Play();
			}
		}
		else
		{
			audio.volume = Mathf.Min(1, audio.volume + Time.deltaTime);
			
			foreach(AudioSource a in audioSourcesToMuteOnTenseMoment)
			{
				a.volume = Mathf.Min(1, audio.volume + Time.deltaTime);
			}
			
			if(characterAtt.hp > 85)
			{
				if(heartSource.clip != deadOnAlert)
				{
					heartSource.clip = deadOnAlert;
					heartSource.Play();
				}
			}
			else
			{
				if(heartSource.clip != null)
				{
					heartSource.Stop();
					heartSource.clip = null;
				}
			}
		}
	}
	
	/// <summary>
	/// Esperar entre as batidas
	/// </summary>
	private IEnumerator WaitBetweenBeats()
	{
		yield return new WaitForSeconds(timeBetweenBeat);
		
		if(characterAtt.hp > 0 && characterAtt.hp <= maxToNormalWork)
		{
			rings1.Play("tum1");
			rings2.Play("tum1");
			rings3.Play("tum1");
		}
		
		StartCoroutine(WaitBetweenDoubleBeats());
	}
	
	/// <summary>
	/// Esperar entre a dupla batidas
	/// </summary>
	private IEnumerator WaitBetweenDoubleBeats()
	{
		yield return new WaitForSeconds(timeBetweenDouble);
		
		if(characterAtt.hp > 0 && characterAtt.hp <= maxToNormalWork)
		{
			rings1.Play("tum2");
			rings2.Play("tum2");
			rings3.Play("tum2");
		}
		
		StartCoroutine(WaitBetweenBeats());
	}
}
